package 二分;

import java.util.Arrays;

/**
 * @author mubei
 * @date 2021/12/20
 * @description
 */
public class Demo01供暖器 {


    public int findRadius(int[] houses, int[] heaters) {
        int l = 0, r = 30;
        Arrays.sort(houses);
        Arrays.sort(heaters);
        while (l < r) {
            int mid = (r - l) / 2 + l;
            int index = 0;
            boolean flag = true;
            to:for (int i = 0; i < houses.length; i++) {
                while(Math.abs(houses[i] - heaters[index])>mid){
                    index++;
                    if(index==heaters.length){
                        flag = false;
                        break to;
                    }
                }
            }
            if(flag){
                r = mid;
            }else{
                l = mid + 1;
            }
        }

        return l;
    }

    public static void main(String[] args) {
        Demo01供暖器 test = new Demo01供暖器();
        int[] houses = {1,5};
        int[] heaters = {10};
        System.out.println(test.findRadius(houses, heaters));

    }

}
